﻿Public Class NhapXuatDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    'Them Nhap
    Public Function InNhap(ByVal nx As NhapXuatDT)
        Dim ma As String = nx.Ma
        Dim maNV As String = nx.MaNV
        Dim ngay As Date = nx.Ngay
        sql = "insert into SanPham_Nhap(MaNhap,ngayNhap,MaNV) values ('" + ma + "','" + ngay + "','" + maNV + "')"
        Return cn.Execute(sql)
    End Function

    'Them,CN Xuat
    Public Function InUpXuat(ByVal nx As NhapXuatDT, ByVal flagIsUp As Boolean)
        Dim ma As String = nx.Ma
        Dim maNV As String = nx.MaNV
        Dim ngay As Date = nx.Ngay
        Dim tongtien As Double = nx.TongTien
        If flagIsUp = True Then
            sql = "insert into SanPham_Xuat(MaXuat,ngayXuat,MaNV,tongtien) values ('" + ma + "','" + ngay + "','" + maNV + "', " & tongtien & ")"
        Else
            sql = "UPDATE SanPham_Xuat SET tongtien = " & tongtien & " WHERE MaXuat = '" + ma + "'"
        End If
        Return cn.Execute(sql)
    End Function

    'Get ID
    Public Function GetID(ByVal _bang As String) As Integer
        Dim i As Integer = 1
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM" + _bang)
        If dt.Rows.Count > 0 Then
            Convert.ToInt32(dt.Rows(dt.Rows.Count - 1).Item("Id"))
        End If
        Return i
    End Function

    'Get Ma
    Public Function GetMa(ByVal _bang As String, ByVal ngay As String, ByVal ngayNhaplieu As Date, ByVal hinhthuc As String) As String
        Dim ma As String = ""
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM" + _bang + " WHERE " + ngay + " = '" + ngayNhaplieu + "'")
        If dt.Rows.Count > 0 Then
            ma = dt.Rows(dt.Rows.Count - 1).Item(hinhthuc).ToString()
        End If
        Return ma
    End Function

    'Get TongTien
    Public Function getTongTien(ByVal maxuat As String) As Double
        Dim tongtien As Double = 0
        Dim dt As New DataTable
        dt = cn.GetData("SELECT tongtien FROM SanPham_Xuat WHERE MaXuat = '" + maxuat + "'")
        If dt.Rows.Count > 0 Then
            tongtien = Convert.ToDouble(dt.Rows(0)("tongtien"))
        End If
        Return tongtien
    End Function
End Class
